package com.fowo.api.ptt.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.ptt.enitty.PrintTemplateType;
import com.fowo.api.ptt.model.PrintTemplateLoadData;
import com.fowo.api.ptt.model.PrintTemplateLoadSql;
import com.fowo.api.ptt.service.PrintTemplateTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 打印类型控制器
 *
 * @author yl_ls
 */
@Tag(name = "PrintTemplateType", description = "打印类型")
@RestController
@RequestMapping("api/sys/print-template-type")
public class PrintTemplateTypeController {
    @Resource
    private PrintTemplateTypeService printTemplateTypeService;

    @Operation(summary = "创建类型")
    @PreAuthorize("@am.canAccess('printTemplateTypeCreate')")
    @PostMapping
    public R<?> create(@RequestBody PrintTemplateType printTemplateType) {
        return R.data(() -> printTemplateTypeService.create(printTemplateType));
    }

    @Operation(summary = "更新类型")
    @PreAuthorize("@am.canAccess('printTemplateTypeUpdate')")
    @PutMapping
    public R<?> update(@RequestBody PrintTemplateType printTemplateType) {
        return R.run(() -> printTemplateTypeService.update(printTemplateType));
    }

    @Operation(summary = "删除类型")
    @PreAuthorize("@am.canAccess('printTemplateTypeDelete')")
    @DeleteMapping("{id}")
    public R<?> delete(@PathVariable Long id) {
        return R.run(() -> printTemplateTypeService.delete(id));
    }

    @Operation(summary = "报表获取真实打印数据接口")
    @PostMapping("data")
    public R<?> getPrintData(@RequestBody PrintTemplateLoadData templateLoadData) {
        return R.data(printTemplateTypeService.getPrintData(templateLoadData));
    }

    @Operation(summary = "数据获取类型sql时预览接口")
    @PreAuthorize("@am.canAccess('printTemplateTypeCreate,printTemplateTypeUpdate')")
    @PostMapping("runSql")
    public R<?> runSql(@RequestBody PrintTemplateLoadSql printTemplateLoadSql) {
        return R.data(() -> printTemplateTypeService.runSql(printTemplateLoadSql));
    }
}
